import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

import javax.usb.*;
import javax.usb.event.UsbServicesEvent;
import javax.usb.event.UsbServicesListener;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.List;


import static jdk.nashorn.internal.runtime.regexp.joni.Syntax.Java;

/*通过线程监听usb端口 获得插拔的状态*/
public class Main {
    private static short idVendor = (short) 0x05AC;
    private static short idProduct = (short) 0x12A8;

    public static void main(String[] args) throws Exception {
        System.out.println("hello");


        Observable findUsb = Observable.create(emitter -> {
            Thread thread = new Thread(() -> {
                try {
                    while (true) {
                        UsbDevice device = findDevice(UsbHostManager.getUsbServices().getRootUsbHub());
                        if (device != null) {
                            emitter.onNext(device);
                        }
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
            thread.start();

        });

        Observer<UsbDevice> doUsb = new Observer<UsbDevice>() {
            @Override
            public void onSubscribe(Disposable disposable) {

            }

            @Override
            public void onNext(UsbDevice usbDevice) {
                System.err.println("对USB进行操作》》》》");

            }


            @Override
            public void onError(Throwable throwable) {

            }

            @Override
            public void onComplete() {

            }
        };

        findUsb.subscribe(doUsb);


    }


    public static boolean linkDevice() throws Exception {

        UsbDevice device = null;
        device = findDevice(UsbHostManager.getUsbServices()
                .getRootUsbHub());
        if (device == null)
            return false;
        return true;
    }

    /*判断USB设备是手机*/
/*遍历device 获取设备上的所有配置，获得USB所有接口 从接口处获得USB的分类定义 即兼容ID进行判断
 基类：0xff 子类：0x42 0x01 且有序列号serialNumber: 为安卓设备。*/
    public static UsbDevice findDevice(UsbHub hub) throws UsbException {
        UsbDevice device = null;
        /*遍历设备 */
        List list = (List) hub.getAttachedUsbDevices();
        for (int i = 0; i < list.size(); i++) {
            device = (UsbDevice) list.get(i);
            Byte serialNumber = device.getUsbDeviceDescriptor().iSerialNumber();

            /*序列号不为0 开始下一步判断
            * 遍历得到接口中的描述
            * */
            if (!serialNumber.toString().equals("0")) {
                System.err.println(i + "__序列号为》》"+serialNumber);
                UsbInterface iface = null;
                List<UsbConfiguration> configurationList = device.getUsbConfigurations();
                /*多配置 遍历*/
                for (int j = 0; j < configurationList.size(); j++) {
                    UsbConfiguration usbConfiguration = configurationList.get(j);
                   // System.out.println(j + "_____-配置中的描述为》》》" + usbConfiguration.getUsbConfigurationDescriptor().iConfiguration());
                      /*多接口 遍历 是否满足 0xff 0x42 0x01*/
                    for(int k=0;k<usbConfiguration.getUsbInterfaces().size();k++){
                        iface = (UsbInterface) usbConfiguration.getUsbInterfaces().get(k);
                        // System.out.println(i+"___接口>>>>"+iface.getUsbInterfaceDescriptor());
                        if (iface != null) {
                            Byte baseClassID = iface.getUsbInterfaceDescriptor().bInterfaceClass();  //基类
                            Byte subClassID = iface.getUsbInterfaceDescriptor().bInterfaceSubClass();//子类
                            Byte protocolID = iface.getUsbInterfaceDescriptor().bInterfaceProtocol();//协议
                            // System.out.println("兼容ID>>>>" + baseClassID);
                            if(baseClassID.toString().equals("-1")&&subClassID.toString().equals("66")&&protocolID.toString().equals("1")){
                                System.err.println("安卓手机连接了》》》》");
                                return device;
                            }else if(baseClassID.toString().equals("6")){
                                System.err.println("苹果设备连接了》》》》");
                                return device;
                            }else{
                                System.err.println("设备无法确定类型》》》》");
                            }
                            /*   if (baseClassID.toString().equals("2") || baseClassID.toString().equals("6") || baseClassID.toString().equals("10") || baseClassID.toString().equals("-1")) {
                             *//*应该再取得USB的VIP PID 与映射表进行对比 精确确认 类型是否为手机*//*
                        System.err.println("手机连接了了》》》");
                        return device;
                    }*/

               /*  if (desc.idVendor() == idVendor && desc.idProduct() == idProduct) {
                    System.err.println("手机连接了了》》》");
                    return device;
                }*/
                        }
                    }
                }

                //UsbDeviceDescriptor desc = device.getUsbDeviceDescriptor();
              //  System.out.println(i + "___idVendor___" + desc.idVendor() + "___idProduct___" + desc.idProduct());

            }
               else {
                System.out.println(i + "__序列号为》》为零" );
            }

        }
        return null;
    }
}
